package com.mywang.weitravel.modules.find;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mywang.weitravel.R;
import com.mywang.weitravel.base.BaseActivity;
import com.mywang.weitravel.model.FindDetailResponse;
import com.mywang.weitravel.modules.image.GlideImageEngine;
import com.mywang.weitravel.util.ScreenUtil;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class FindDetailActivity extends BaseActivity<FindDetailPresenter> implements FindDetailContract.View {

    public static void launch(Activity activity, String findId) {
        Intent intent = new Intent(activity, FindDetailActivity.class);
        intent.putExtra("findId", findId);
        launchActivity(activity, intent);
    }

    @BindView(R.id.iv_title_back)
    ImageView ivTitleBack;
    @BindView(R.id.mz_banner)
    MZBannerView<String> mzBanner;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.iv_action)
    ImageView ivAction;

    private String findId;

    @Override
    protected void initView() {
        findId = getIntent().getStringExtra("findId");
        mPresenter.getFindDetail(findId);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mzBanner.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mzBanner.pause();
    }

    @OnClick({R.id.iv_title_back, R.id.iv_action})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.iv_action:
                if (goLoginActivity(this)) {
                    mPresenter.likeFind(findId);
                }
                break;
        }
    }

    @Override
    protected FindDetailPresenter initPresenter() {
        return new FindDetailPresenter(this);
    }

    @Override
    protected int getActivityLayoutID() {
        return R.layout.activity_find_detail;
    }

    @Override
    public void likeFindSuccessful() {
        ivAction.setImageResource(R.mipmap.ic_like);
    }

    private void initBanner() {
        ArrayList<String> banners = new ArrayList<>();
        banners.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1576722562&di=e09585dee1cd439965d8f79b275f2226&imgtype=jpg&er=1&src=http%3A%2F%2Fwww.wallcoo.com%2Fnature%2F2010_Landscape_1680_Desktop_08%2Fwallpapers%2F1280x800%2FBavaria%2520Germany.jpg");
        banners.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1576127843998&di=cc8edadd24664c0171651b2169eb769b&imgtype=0&src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fwallpaper%2F1212%2F12%2Fc0%2F16543373_1355292820262.jpg");
        banners.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1576127843998&di=5bc33b158db4048a2e92a0b13ceab741&imgtype=0&src=http%3A%2F%2Fs9.knowsky.com%2Fbizhi%2Fl%2F1-5000%2F2009528162820841730534.jpg");
        mzBanner.setIndicatorVisible(false);
        mzBanner.setPages(banners, new MZHolderCreator<BannerHolder>() {
            @Override
            public BannerHolder createViewHolder() {
                return new BannerHolder();
            }
        });
    }

    @Override
    public void getFindDetailSuccessful(FindDetailResponse findDetailResponse) {
        if (findDetailResponse.url != null && !findDetailResponse.url.isEmpty()) {
            mzBanner.setIndicatorVisible(false);
            mzBanner.setPages(findDetailResponse.url, new MZHolderCreator<BannerHolder>() {
                @Override
                public BannerHolder createViewHolder() {
                    return new BannerHolder();
                }
            });
            mzBanner.start();
        }
        GlideImageEngine.getInstance().loadImage(findDetailResponse.avatar, ivAvatar, R.mipmap.icon_head_default, ScreenUtil.dp2px(12));
        tvTitle.setText(findDetailResponse.title);
        tvContent.setText(findDetailResponse.content);
        tvUserName.setText(findDetailResponse.username);
        if (findDetailResponse.isSupport()) {
            ivAction.setImageResource(R.mipmap.ic_like);
        } else {
            ivAction.setImageResource(R.mipmap.ic_unlike);
        }
    }

    public static class BannerHolder implements MZViewHolder<String> {

        private ImageView ivBanner;

        @Override
        public View createView(Context context) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_main_banner, null);
            ivBanner = view.findViewById(R.id.iv_banner);
            return view;
        }

        @Override
        public void onBind(Context context, int position, String data) {
            // 数据绑定
            GlideImageEngine.getInstance().loadImage(data, ivBanner);
        }
    }

}
